package day0213;

import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {
        /*
            二分搜索
            分治
         */

        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};

        Scanner in = new Scanner(System.in);

        System.out.println("请输入要查询的数字：");
        int num = in.nextInt();

        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int middle = (left + right) >> 1;

            if (arr[middle] == num) {
                System.out.println("下标是: " + middle);
                break;
            } else if (arr[middle] > num) {
                right = middle - 1;
            } else {
                left = middle + 1;
            }

        }
    }
}
